var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		diaLog: 0,
		// tanchuhongbao: false,
		answerIndex: 0,
		level: 0,
		chuigeerrIndex: [],
		resultStatus: !1,
		sharetype: "guess",
		openid: '',
		userRes: {},
		jinbi:0,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.questionlist();
		const db = wx.cloud.database();
		if (app.globalData.openid) {
			this.setData({
				openid: app.globalData.openid
			})
			db.collection('user').where({
				user_openid: this.data.openid
			}).get({
				success: res => {
					this.setData({
						userRes: res.data[0]
					})
				},
				fail: err => {
					wx.showToast({
						icon: 'none',
						title: '查询记录失败'
					})
					console.error('[数据库-user] [查询记录] 失败：', err)
				}
			})
		} else {
			//获取用户openId
			wx.cloud.callFunction({
				name: 'login',
				success: res => {
					app.globalData.openid = res.result.openid;
					this.setData({
						openid: res.result.openid
					})
					db.collection('user').where({
						user_openid: res.result.openid
					}).get({
						success: res => {
							this.setData({
								userRes: res.data[0]
							})
						},
						fail: err => {
							wx.showToast({
								icon: 'none',
								title: '查询记录失败'
							})
							console.error('[数据库-user] [查询记录] 失败：', err)
						}
					})
				},
				fail: err => {
					console.error('[云函数] [login] 调用失败：', err);
				}
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	chuigeLoginCancel: function() {
		this.setData({
			chuigeloginStatus: !1
		});
	},
	chuigeshowLogin: function() {
		this.setData({
			chuigeloginStatus: !0
		});
	},
	chuigeLoginConfirm: function(e) {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},
	/**
	 * 请求问题库
	 *获取问题跟答案 
	 */
	questionlist: function() {
		var e = this;
		const db = wx.cloud.database()
		db.collection('question').aggregate().sample({
			size: 11
		}).end().then(res => {
			var data = res.list;
			"collection.aggregate:ok" == res.errMsg && (
				res.list.length > 0 ? e.setData({
					resultStatus: !1,
					questionlist: data,
					answerIndex: 0,
					question: data[0]
				}) : e.setData({
					resultStatus: !1,
					noMoreTopic: !0
				}));
		});
	},
	/**
	 * @param {Object} options
	 * 选择答案
	 */
	answerCheck: function(options) {
		var indexs = options.target.dataset.id;
		// 回答正确
		if (indexs == this.data.question.daan) {
			if (this.data.level == 9) {
				//全部回答正确 跳转到领奖页面
				this.updateUserLeve(this.data.userRes._id, this.data.level, this.data.userRes.jinbi);
			} else {
				//更新用户数据
				this.setData({
					diaLog: 1,
				})
			}
		} else {
			this.updateUserLeve(this.data.userRes._id, this.data.level, false,this.data.userRes.jinbi);
			// 回答错误去首页
			this.setData({
				diaLog: 2,
			})
		}
	},

	/**
	 * 进入下一关
	 */
	goDown: function() {
		if (this.data.answerIndex >= this.data.questionlist.length - 1) {
			this.setData({
				question: this.data.questionlist[0],
				diaLog: 0,
				chuigeerrIndex: [],
				level: this.data.level + 1,
				answerIndex: 0
			})
		} else {
			this.setData({
				question: this.data.questionlist[this.data.answerIndex + 1],
				diaLog: 0,
				answerIndex: this.data.answerIndex + 1,
				level: this.data.level + 1,
			})
		}

	},

	/**
	 * 回答错误跳转首页
	 */
	goHome: function() {
		//跳转到游戏页面
		wx.redirectTo({
			url: "/pages/index/index"
		});
	},

	/**
	 * 通过id更新排行
	 */
	updateUserLeve(id, level, info, jinbi) {
		// console.log(jinbi, "jinbi");
		let newJinbi = 0;
		if (info) {
			newJinbi = jinbi + 10;
		} else {
			newJinbi = jinbi;
		}
		const db = wx.cloud.database();
		db.collection('user').doc(id).update({
			data: {
				level: level,
				jinbi: newJinbi
			},
			success: res => {
				if (info) {
					// console.log(res, "闯关层数");
					//跳转到游戏页面
					wx.navigateTo({
						url: "/pages/success/success"
					});
				} else {
					console.log(res, "闯关层数");
				}
			},
			fail: err => {
				icon: 'none',
				console.error('[数据库] [更新记录] 失败：', err)
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
	onShareTimeline: function() {

	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
